package io.renren.service.impl;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.dao.CtocOrderDao;
import io.renren.entity.CtocOrderEntity;
import io.renren.service.CtocOrderService;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

@Service("ctocOrderService")
public class CtocOrderServiceImpl extends ServiceImpl<CtocOrderDao, CtocOrderEntity> implements CtocOrderService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		Object status = params.get("status");
		Object memberId = params.get("memberId");
		String orderNo = (String) params.get("orderNo");
		String quantity = (String) params.get("quantity");
		int q1 = -1;
		int q2 = -1;
		if (StringUtils.isNotBlank(quantity)) {
			String[] s = quantity.split(",");
			try {
				q1 = Integer.valueOf(s[0]);
				q2 = Integer.valueOf(s[1]);
			} catch (Exception e) {
			}
		}

		EntityWrapper<CtocOrderEntity> wrapper = new EntityWrapper<CtocOrderEntity>();
		wrapper.eq(status != null, "status", status).eq(memberId != null, "member_id", memberId).between(q1 != -1 && q2 != -1, "coin_quantity", q1, q2);
		if (StringUtils.isNotBlank(orderNo)) {
			wrapper.andNew().eq("order_no", orderNo).or().eq("mobile", orderNo);
		}
		wrapper.orderBy("coin_price", false);
		Page<CtocOrderEntity> page = this.selectPage(new Query<CtocOrderEntity>(params).getPage(), wrapper);

		return new PageUtils(page);
	}
}
